/*     */ package org.yaukie.auth.service.impl;
/*     */ 
/*     */ import eu.bitwalker.useragentutils.UserAgent;
/*     */ import io.jsonwebtoken.Claims;
/*     */ import io.jsonwebtoken.Jwts;
/*     */ import io.jsonwebtoken.SignatureAlgorithm;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.concurrent.TimeUnit;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.beans.factory.annotation.Value;
/*     */ import org.springframework.security.core.Authentication;
/*     */ import org.springframework.security.core.context.SecurityContextHolder;
/*     */ import org.springframework.security.core.userdetails.UserDetails;
/*     */ import org.springframework.stereotype.Component;
/*     */ import org.yaukie.auth.entity.LoginUser;
/*     */ import org.yaukie.auth.service.api.TokenHandlerService;
/*     */ import org.yaukie.base.redis.RedisOrMapCache;
/*     */ import org.yaukie.base.util.IpUtil;
/*     */ import org.yaukie.base.util.SpringContextUtil;
/*     */ import org.yaukie.base.util.StringTools;
/*     */ import org.yaukie.base.uuid.IdUtils;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ @Component
/*     */ public class TokenHandlerServiceImpl
/*     */   implements TokenHandlerService
/*     */ {
/*  39 */   private static final Logger log = LoggerFactory.getLogger(TokenHandlerServiceImpl.class);
/*     */ 
/*     */   
/*     */   @Autowired
/*     */   private RedisOrMapCache redisCache;
/*     */ 
/*     */   
/*     */   @Value("${yaukie.token.header:Authorization}")
/*     */   private String tokenHeader;
/*     */ 
/*     */   
/*     */   @Value("${yaukie.token.expire:10}")
/*     */   private String tokenExpired;
/*     */   
/*     */   @Value("${yaukie.token.secret:}")
/*     */   private String secret;
/*     */   
/*     */   @Value("${yaukie.token.refresh:1}")
/*     */   private String refreshExpired;
/*     */ 
/*     */   
/*     */   public String buildToken(LoginUser loginUser) {
/*  61 */     String token = IdUtils.fastUUID();
/*  62 */     loginUser.setToken(token);
/*  63 */     long current = System.currentTimeMillis();
/*  64 */     long expried = current + Long.parseLong(this.tokenExpired) * 60L * 1000L;
/*  65 */     loginUser.setExpireTime(Long.valueOf(expried));
/*  66 */     loginUser.setLoginTime(new Date());
/*  67 */     setUserAgent(loginUser);
/*  68 */     refreshToken(loginUser);
/*  69 */     Map dataMap = new HashMap();
/*  70 */     dataMap.put("login_user_key", token);
/*  71 */     return Jwts.builder().setClaims(dataMap)
/*  72 */       .signWith(SignatureAlgorithm.HS512, "yuenbinisagreatman!")
/*  73 */       .compact();
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public boolean verifyToken(HttpServletRequest request) {
/*  84 */     if (log.isInfoEnabled()) {
/*  85 */       log.info("令牌验证开始....");
/*     */     }
/*     */     
/*  88 */     String token = getToken(request);
/*     */     
/*  90 */     if (StringTools.isEmpty(token)) {
/*  91 */       return false;
/*     */     }
/*     */     
/*  94 */     LoginUser loginUser = (LoginUser)this.redisCache.getCacheObject(getTokenKey(token));
/*     */     
/*  96 */     if (StringTools.isNull(loginUser)) {
/*  97 */       return false;
/*     */     }
/*     */     
/* 100 */     long expired = loginUser.getExpireTime().longValue();
/* 101 */     long current = System.currentTimeMillis();
/* 102 */     if (current > expired) {
/*     */       
/* 104 */       log.info("用户会话标识[token]已经过期...");
/* 105 */       return false;
/*     */     } 
/*     */     
/* 108 */     if (expired - current <= Long.parseLong(this.refreshExpired)) {
/* 109 */       refreshToken(loginUser);
/* 110 */       log.info("登录令牌{}刷新成功", token);
/*     */     } 
/*     */ 
/*     */     
/* 114 */     if (log.isInfoEnabled()) {
/* 115 */       log.info("令牌验证结束....");
/*     */     }
/* 117 */     return true;
/*     */   }
/*     */ 
/*     */   
/*     */   public void refreshToken(LoginUser loginUser) {
/* 122 */     String token = loginUser.getToken();
/* 123 */     loginUser.setExpireTime(Long.valueOf(System.currentTimeMillis() + (Integer.parseInt(this.tokenExpired) * 60 * 1000)));
/* 124 */     String tokenKey = getTokenKey(token);
/* 125 */     this.redisCache.setCacheObject(tokenKey, loginUser, Integer.valueOf(Integer.parseInt(this.tokenExpired)), TimeUnit.MINUTES);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public void delLoginUser(String token) {
/* 136 */     if (StringTools.isNotEmpty(token)) {
/* 137 */       String tokenKey = getTokenKey(token);
/*     */       
/* 139 */       this.redisCache.deleteObject(tokenKey);
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public String getToken(HttpServletRequest request) {
/* 152 */     String token = request.getHeader(this.tokenHeader);
/* 153 */     if (StringTools.isEmpty(token)) {
/* 154 */       token = request.getHeader("token");
/* 155 */     } else if (token.contains("Bearer ") && (
/* 156 */       token.startsWith("Bearer ") || token.indexOf("Bearer ") != -1)) {
/*     */       
/* 158 */       token = token.replace("Bearer ", "");
/* 159 */       Claims claims = parseToken(token);
/* 160 */       token = claims.get("login_user_key") + "";
/*     */     } 
/*     */     
/* 163 */     if (StringTools.isEmpty(token)) {
/* 164 */       token = "";
/*     */     }
/* 166 */     return token;
/*     */   }
/*     */ 
/*     */   
/*     */   public Claims parseToken(String jwtToken) {
/* 171 */     return (Claims)Jwts.parser()
/* 172 */       .setSigningKey("yuenbinisagreatman!")
/* 173 */       .parseClaimsJws(jwtToken)
/* 174 */       .getBody();
/*     */   }
/*     */ 
/*     */ 
/*     */   
/*     */   public LoginUser getLoginUser(HttpServletRequest request) {
/* 180 */     LoginUser loginUser = null;
/* 181 */     String token = getToken(request);
/* 182 */     if (!isTokenExpired(token)) {
/* 183 */       loginUser = (LoginUser)this.redisCache.getCacheObject(getTokenKey(token));
/*     */     }
/* 185 */     return loginUser;
/*     */   }
/*     */ 
/*     */   
/* 189 */   public String getTokenKey(String uid) { return "login_token_" + uid; }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public Authentication getAuthenticationFromToken(HttpServletRequest request) {
/* 200 */     Authentication authentication = null;
/* 201 */     String token = getToken(request);
/* 202 */     if (StringTools.isEmpty(token)) {
/* 203 */       return null;
/*     */     }
/*     */     
/* 206 */     if (!isTokenExpired(token)) {
/* 207 */       authentication = SecurityContextHolder.getContext().getAuthentication();
/*     */     }
/* 209 */     return authentication;
/*     */   }
/*     */ 
/*     */   
/*     */   public String getUserNameFromAuthentication(HttpServletRequest request) {
/* 214 */     Authentication authentication = getAuthenticationFromToken(request);
/* 215 */     String userName = null;
/* 216 */     if (null != authentication) {
/* 217 */       Object principal = authentication.getPrincipal();
/* 218 */       if (principal != null && principal instanceof UserDetails) {
/* 219 */         userName = ((UserDetails)principal).getUsername();
/*     */       }
/*     */     } 
/* 222 */     return userName;
/*     */   }
/*     */ 
/*     */   
/*     */   public boolean isTokenExpired(String token) {
/* 227 */     LoginUser loginUser = (LoginUser)this.redisCache.getCacheObject(getTokenKey(token));
/*     */     
/* 229 */     if (StringTools.isNull(loginUser)) {
/* 230 */       return true;
/*     */     }
/*     */     
/* 233 */     long expired = loginUser.getExpireTime().longValue();
/* 234 */     long current = System.currentTimeMillis();
/* 235 */     if (current > expired)
/*     */     {
/* 237 */       return true;
/*     */     }
/*     */     
/* 240 */     return false;
/*     */   }
/*     */ 
/*     */ 
/*     */   
/*     */   private void setUserAgent(LoginUser loginUser) {
/* 246 */     UserAgent userAgent = UserAgent.parseUserAgentString(SpringContextUtil.getRequest().getHeader("User-Agent"));
/* 247 */     loginUser.setBrowser(userAgent.getBrowser().getName());
/* 248 */     loginUser.setOs(userAgent.getOperatingSystem().getName());
/* 249 */     loginUser.setIpaddr(IpUtil.getIpAddr(SpringContextUtil.getRequest()));
/* 250 */     loginUser.setLoginLocation(IpUtil.getRealAddressByIP(IpUtil.getIpAddr(SpringContextUtil.getRequest())));
/*     */   }
/*     */ }


/* Location:              /Users/rex/Desktop/x-common-auth-1.0.0-SNAPSHOT.jar!/org/yaukie/auth/service/impl/TokenHandlerServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.0.7
 */